package com.woniuxy.carlife.carlifeuserweb.util;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.RandomCode;
import io.jsonwebtoken.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author zjq
 * @ClassName:SendAuthCodeUtil
 * @Description:发送短信验证码的工具类
 * @date 2021/4/7 11:56
 **/

public class SendAuthCodeUtil {
    static class SendCodeParam {
        public String code;
        public String phoneNum;

        public void setCode(String code) {
            this.code = code;
        }

        public void setPhoneNum(String phoneNum) {
            this.phoneNum = phoneNum;
        }
    }

    /*
     * @author LiuXiaoXian
     * @since  2021-04-06
     * @description  发送验证码的接口
     * @param  传入电话
     * @return  正常发送后，返回验证码
     */
    public static String sendAuthCode(String tel) {
        // 获得Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URI uri = null;
        try {// 设置uri信息
            uri = new URIBuilder().setScheme("http")
                    .setHost("pub.woniulab.com").
                            setPort(8081).
                            setPath("/msg/sendMsg").
                            build();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }
        SendCodeParam param = new SendCodeParam();
        String code = RandomUtil.randomNumbers(4);
        param.setCode(code);
        //支持[0-9][a-z][A-Z]
        param.setPhoneNum(tel);
        //发送手机号码
        // 创建Post请求
        HttpPost httpPost = new HttpPost(uri);
        StringEntity entity = new StringEntity(JSON.toJSONString(param), "UTF-8");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        // 响应模型 12345678910111213141516171819202122
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
        return code;
    }

}
